<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:replace="~{common/header :: commonHead}">
    <title>活动记录</title>
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight" th:with="page=${pageObj.page},size=${pageObj.pageSize},total=${pageObj.total},totalPage=${pageObj.totalPage},keyword=${pageObj.keyword},fromDate=${pageObj.fromDate},toDate=${pageObj.toDate}">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>活动记录</h5>
                    <div class="ibox-tools">
                        <a href="javascript: window.location.href='/marketActivity/scratchCard/record';" title="刷新"><i class="glyphicon glyphicon-repeat"></i></a>
                    </div>
                </div>
                <div class="ibox-content">
                    <div id="editable_wrapper" class="dataTables_wrapper form-inline" role="grid">
                        <div class="row">
                            <div class="col-sm-6">
                                <div class="col-sm-6">
                                    <div class="dataTables_length" id="editable_length"><label>每页
                                        <select name="editable_length" aria-controls="editable" class="form-control input-sm" id="page_size_selector">
                                            <option th:selected="${size eq 5}" value="5">5</option>
                                            <option th:selected="${size eq 10}" value="10">10</option>
                                            <option th:selected="${size eq 25}" value="25">25</option>
                                            <option th:selected="${size eq 50}" value="50">50</option>
                                            <option th:selected="${size eq 100}" value="100">100</option>
                                        </select> 条记录</label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <table class="table table-striped table-bordered table-hover  dataTable" id="editable"
                               aria-describedby="editable_info">
                            <thead>
                            <tr role="row">
                                <th aria-label="ID" style="width: 50px;">ID</th>
                                <th aria-label="活动名称" style="width: 347px;">活动名称</th>
                                <th aria-label="活动开始时间" style="width: 320px;">活动开始时间</th>
                                <th aria-label="活动结束时间" style="width: 137px;">活动结束时间</th>
                                <th aria-label="创建时间" style="width: 137px;">创建时间</th>
                                <th aria-label="状态" style="width: 137px;">状态</th>
                                <th aria-label="操作" style="width: 140px;">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr class="gradeA odd" th:if="${#lists.isEmpty(pageObj.list)}">
                                <td colspan="9" style="text-align: center">
                                    没有数据o&gt;^&lt;o
                                </td>
                            </tr>
                            <tr class="gradeA odd" th:each="item : ${pageObj.list}">
                                <td th:text="${item.id}" ></td>
                                <td th:with="name=${item.name}" th:utext="${null == keyword ? name : #strings.replace(name,keyword,'<span style= &quot;color: red;font-weight:bold&quot;>'+ keyword +'</span>')}"></td>
                                <td th:text="${item.startTime}"></td>
                                <td th:text="${item.endTime}"></td>
                                <td th:text="${item.createTime}"></td>
                                <td th:if="${item.status == 1}">上架</td>
                                <td th:if="${item.status == 0}">下架</td>
                                <td class="center " >
                                    <shiro:hasPermission name="edit_store">
                                        <a type="button" th:href="${'/marketActivity/scratchCard/gift?id=' + item.id}" title="奖项"><i class="glyphicon glyphicon-gift"></i></a>
                                    </shiro:hasPermission>
                                    <shiro:hasPermission name="edit_store">
                                        <a type="button" th:href="${'/marketActivity/scratchCard/record?id=' + item.id}" title="记录"><i class="glyphicon glyphicon-list"></i></a>
                                    </shiro:hasPermission>
                                    <shiro:hasPermission name="edit_store">
                                        <a type="button" th:href="${'/marketActivity/scratchCard/add?id=' + item.id}" title="编辑"><i class="glyphicon glyphicon-edit"></i></a>
                                    </shiro:hasPermission>
                                    <shiro:hasPermission name="delete_store">
                                        <a type="button" th:onclick="deleteOne(([[${item.id}]]))" title="删除"><i class="glyphicon glyphicon-trash"></i></a>
                                    </shiro:hasPermission>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                        <div class="row">
                            <div class="col-sm-6">
                                <div class="dataTables_info" role="alert" aria-live="polite" aria-relevant="all"
                                     th:text="${'显示 '
                                      + (page * size + 1)
                                      + ' 到 '
                                      + ((page+1) lt totalPage ? (size + page * size) : (total % size == 0 ? total : (total % size + size * page)))
                                      + ' 项，共 '
                                      + total + ' 项 ' + totalPage + ' 页'}">
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="dataTables_paginate paging_simple_numbers">
                                    <ul class="pagination" th:if="${totalPage>1}">
                                        <li th:class="${page==0}?'paginate_button previous disabled':'paginate_button previous'">
                                            <a th:href="${(page==0)?'#':'/marketActivity/scratchCard/list?page='+page
                                             +'&pageSize='+size
                                             +'&keyword='+(null==keyword?'':keyword)
                                             +'&fromDate='+(null==fromDate?'':fromDate)
                                             +'&toDate='+(null==toDate?'':toDate)}">上一页</a>
                                        </li>
                                        <li th:if="${totalPage<10}" th:each="i:${#numbers.sequence(1,totalPage)}"
                                            th:class="${i==page+1?'active':''}">
                                            <a th:href="${'/marketActivity/scratchCard/list?page='+i
                                            +'&pageSize='+size
                                            +'&keyword='+ (null==keyword?'':keyword)
                                            +'&fromDate='+(null==fromDate?'':fromDate)
                                            +'&toDate='+(null==toDate?'':toDate)}"
                                               th:text="${i}" ></a>
                                        </li>
                                        <li th:if="${page<4&&totalPage>=9}" th:each="i:${#numbers.sequence(1,9)}"
                                            th:class="${i==page+1?'active':''}">
                                            <a th:href="${'/marketActivity/scratchCard/list?page='+i
                                            +'&pageSize='+size
                                            +'&keyword='+ (null==keyword?'':keyword)
                                            +'&fromDate='+(null==fromDate?'':fromDate)
                                            +'&toDate='+(null==toDate?'':toDate)}"
                                               th:text="${i}" ></a>
                                        </li>
                                        <li th:if="${page>=4&&totalPage>=9&&page+5<totalPage}" th:each="i:${#numbers.sequence(page - 3,page + 5)}"
                                            th:class="${i==page+1?'active':''}">
                                            <a th:href="${'/marketActivity/scratchCard/list?page='+i
                                            +'&pageSize='+size
                                            +'&keyword='+ (null==keyword?'':keyword)
                                            +'&fromDate='+(null==fromDate?'':fromDate)
                                            +'&toDate='+(null==toDate?'':toDate)}"
                                               th:text="${i}" ></a>
                                        </li>
                                        <li th:if="${page>=4&&totalPage>=9&&page+5>=totalPage}" th:each="i:${#numbers.sequence(totalPage - 8,totalPage)}"
                                            th:class="${i==page+1?'active':''}">
                                            <a th:href="${'/marketActivity/scratchCard/list?page='+i
                                            +'&pageSize='+size
                                            +'&keyword='+ (null==keyword?'':keyword)
                                            +'&fromDate='+(null==fromDate?'':fromDate)
                                            +'&toDate='+(null==toDate?'':toDate)}"
                                               th:text="${i}" ></a>
                                        </li>
                                        <li th:class="${page+1==totalPage||totalPage<1}?'paginate_button next disabled':'paginate_button next'" >
                                            <a th:href="${(page+1==totalPage||totalPage<1) ? '#' : '/marketActivity/scratchCard/list?page='+(page+2)
                                             +'&pageSize='+size
                                             +'&keyword='+(null==keyword?'':keyword)
                                             +'&fromDate='+(null==fromDate?'':fromDate)
                                             +'&toDate='+(null==toDate?'':toDate)}">下一页</a>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script th:inline="javascript">
    function deleteOne (id) {
        layer.confirm('确定删除？', {
            btn: ['是','否'] //按钮
        }, function(){
            $.get('/marketActivity/scratchCard/delete?id='+id,(res) => {
                if (res.resCode == 0) {
                layer.msg('删除成功');
                window.location.href = "/marketActivity/scratchCard/list";
            } else if (res.resCode == -1) {
                layer.msg('删除失败');
            } else {
                layer.msg('服务端异常，请重试')
            }
        });
        });
    }
    $("#page_size_selector").change(function () {
        let fromDate = $("#start_date").val();
        let toDate = $("#end_date").val();
        let selected = $(this).val();
        window.location.href = initUrl(0,selected,'',fromDate,toDate);
    })
    $("#search_bar").keydown(function (e) {
        let fromDate = $("#start_date").val();
        let toDate = $("#end_date").val();
        if (e.keyCode == 13) {
            let size = $("#page_size_selector").val();
            if (null == size) {
                size = 10;
            }
            let keyword = $(this).val();
            window.location.href = initUrl(0,size,keyword,fromDate,toDate);
        }
    })

    $("#search-btn").click(function () {
        let fromDate = $("#start_date").val();
        let toDate = $("#end_date").val();
        let size = $("#page_size_selector").val();
        if (null == size) {
            size = 10;
        }
        let keyword = $("#search_bar").val();
        window.location.href = initUrl(0,size,keyword,fromDate,toDate);
    })

    function initUrl(page,size,key,fromDate,toDate) {
        if (null == page) {
            page = 0;
        }
        return "/marketActivity/scratchCard/list?page="+ page +"&pageSize=" + size + "&keyword=" + key + "&fromDate=" + fromDate + "&toDate=" + toDate
    }
</script>
</html>